package cn.edu.sgu.www.mhxysy.function.computer.attribute.impl;

import cn.edu.sgu.www.mhxysy.entity.chongwu.Chongwu;
import cn.edu.sgu.www.mhxysy.entity.chongwu.ChongwuProperty;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.function.computer.attribute.AttributeComputer;
import cn.edu.sgu.www.mhxysy.mapper.chongwu.ChongwuMapper;
import cn.edu.sgu.www.mhxysy.mapper.chongwu.ChongwuPropertyMapper;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 宠物属性计算器
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Component
public class ChongwuAttributeComputer implements AttributeComputer {

    private final ChongwuMapper chongwuMapper;
    private final ChongwuPropertyMapper chongwuPropertyMapper;

    @Autowired
    public ChongwuAttributeComputer(
            ChongwuMapper chongwuMapper,
            ChongwuPropertyMapper chongwuPropertyMapper) {
        this.chongwuMapper = chongwuMapper;
        this.chongwuPropertyMapper = chongwuPropertyMapper;
    }

    @Override
    public Map<String, Integer> apply(String chongwuId) {
        Chongwu chongwu = chongwuMapper.selectById(chongwuId);

        if (chongwu == null) {
            throw new GlobalException(ResponseCode.CONFLICT, "操作失败，宠物不存在！");
        }

        // 查询全部宠物属性类型
        List<ChongwuProperty> list = chongwuPropertyMapper.selectList(null);

        Map<String, Integer> attribute = new HashMap<>(list.size());

        for (ChongwuProperty chongwuProperty : list) {
            attribute.put(chongwuProperty.getName(), 0);
        }

        return attribute;
    }

}